from pynocaptcha import AkamaiV2Cracker
import requests
from src.service.requests_middler import get_proxies
from src.service.requests_middler import get_abrod_proxies
from loguru import logger
from src.common.logger import get_logger
logger_app = get_logger(__name__)

def get_abck_platform():
    # 774a7426-fcbe-4848-9909-e4be4daca98c
    cracker = AkamaiV2Cracker(
        user_token="774a7426-fcbe-4848-9909-e4be4daca98c",
        href="https://www.zimchina.com/schedules/point-to-point",
        debug=True,
    )
    ret = cracker.crack()
    if ret:
        return ret
    else:
        return 0

def get_abck_requests():
    headers = {
        "User-Token": "774a7426-fcbe-4848-9909-e4be4daca98c",
        "Content-Type": "application/json"
    }
    data = {
        "href": "https://www.zimchina.com/schedules/point-to-point",
    }
    url = 'http://api.nocaptcha.io/api/wanda/akamai/v2'

    res = requests.post(url, headers=headers, json=data)
    if res.status_code == 200:
        js_data = res.json()
        print(js_data)
        if js_data['status'] == 1:
            cookies = {}
            cookies['_abck'] = js_data['data']['_abck']
            cookies['bm_sz'] = js_data['data']['bm_sz']
            return cookies
        else:
            logger.error(f"{js_data['msg']}")
            return 0
    else:
        return 0

def get_abck_personal():
    retry_count = 0
    while retry_count < 3:
        # proxies = get_proxies()
        proxies = get_abrod_proxies()
        if not isinstance(proxies, dict) or 'https' not in proxies:
            logger.error(f"Invalid proxies returned: {proxies}")
            return 0
        json_data = {
            'user_name': 'ZIM_A1B2C3D4E5F6G7H8',
            'akmData': {
                'akm_id': 'ZIM',
                'proxy': proxies['https']
            }
        }
        url = 'http://156.225.22.189:9081/akmV3_server'
        try:
            res = requests.post(url, json=json_data, timeout=8)
            if res.status_code == 200:
                js_data = res.json()
                if not isinstance(js_data, dict) or "akCookies" not in js_data:
                    logger.error(f"Invalid JSON response: {js_data}")
                    return 0
                if js_data.get("akCookies"):
                    _abck = js_data['akCookies']['_abck']
                    if _abck.split("~")[1] == -1:
                        logger.error(f"_abck参数为-1 失效参数")
                        return 0
                    logger.success(f"成功获取_abck: 剩余额度: {js_data['balance']}")
                    logger_app.info(f"成功获取_abck: 剩余额度: {js_data['balance']}")
                    if int(js_data['balance']) > 0:
                        return js_data['akCookies']
                    else:
                        logger.error(f"额度已用完, 请联系管理员")
                        return 0
                else:
                    logger.error(f"获取_abck失败, 排查该平台是否维护中")
                    return 0
            else:
                return 0
        except Exception as e:
            if isinstance(e, requests.exceptions.Timeout):
                logger.error(f"请求超时: {e}")
            else:
                logger.error(f"发生异常: {e}")
            retry_count += 1

    return 0


# print(get_abck_personal())